﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCR
{
    public class LicenseHelper
    {
        /// <summary>
        /// 许可证是否可用
        /// </summary>
        /// <returns></returns>
        private bool IsValidLicenseAvailable()
        {
            return License.Status.Licensed;
        }

        /// <summary>
        /// 获取许可证键值信息
        /// </summary>
        private string ReadAdditonalLicenseInformation()
        {
            string rtnStr = string.Empty;
            if (License.Status.Licensed)
            {
                for (int i = 0; i < License.Status.KeyValueList.Count; i++)
                {
                    string key = License.Status.KeyValueList.GetKey(i).ToString();
                    string value = License.Status.KeyValueList.GetByIndex(i).ToString();

                    rtnStr += key + "-" + value + Environment.NewLine;
                }
            }
            return rtnStr;
        }

        /// <summary>
        /// 获取软件锁定信息
        /// </summary>
        /// <returns></returns>
        private string ReadLockMsg()
        {
            string rtnStr = string.Empty;
            //使用持续时间锁
            bool lock_enabled = License.Status.Evaluation_Lock_Enabled;
            License.EvaluationType ev_type = License.Status.Evaluation_Type;
            int time = License.Status.Evaluation_Time;
            int time_current = License.Status.Evaluation_Time_Current;
            rtnStr += string.Format("是否开启持续时间锁：{0},规定使用最大持续时间{1},现在使用时间{2}\n", lock_enabled.ToString(), time.ToString(), time_current.ToString());

            //到期锁
            bool lock_enabled1 = License.Status.Expiration_Date_Lock_Enable;
            System.DateTime expiration_date = License.Status.Expiration_Date;
            rtnStr += string.Format("是否开启到期锁:{0},到期时间{1}\n", lock_enabled1.ToString(), expiration_date.ToShortTimeString());

            //使用次数锁
            bool lock_enabled2 = License.Status.Number_Of_Uses_Lock_Enable;
            int max_uses = License.Status.Number_Of_Uses;
            int current_uses = License.Status.Number_Of_Uses_Current;
            rtnStr += string.Format("是否开启使用次数锁:{0},最大使用次数{1},当前使用次数{2}\n", lock_enabled2.ToString(), max_uses.ToString(), current_uses.ToString());

            //并发运行锁
            bool lock_enabled3 = License.Status.Number_Of_Instances_Lock_Enable;
            int max_instances = License.Status.Number_Of_Instances;
            rtnStr += string.Format("是否限制并行数量:{0},最大并行数量:{1}\n", lock_enabled3, max_instances.ToString());

            //硬件锁
            bool lock_enabled4 = License.Status.Hardware_Lock_Enabled;
            string lic_hardware_id = "";
            if (lock_enabled)
            {
                lic_hardware_id = License.Status.License_HardwareID;
            }
            rtnStr += string.Format("证书是否开启硬件锁{0},对于特定硬件的有效{1}\n", lock_enabled4.ToString(), lic_hardware_id);
            return rtnStr;
        }

        /// <summary>
        /// 获取机器硬件编号
        /// </summary>
        /// <returns></returns>
        private string GetHardwareID()
        {
            return License.Status.HardwareID;
        }

        /// <summary>
        /// 获取许可证适用的硬件编码
        /// </summary>
        /// <returns></returns>
        private string GetLicenseHardwareID()
        {
            return License.Status.License_HardwareID;
        }

        /// <summary>
        /// 作废许可证
        /// </summary>
        private string InvalidateLicense()
        {
            string confirmation_code = License.Status.InvalidateLicense();
            return confirmation_code;
        }

        /// <summary>
        /// 检查作废许可证的验证码是否有效
        /// </summary>
        /// <param name="confirmation_code"></param>
        /// <returns></returns>
        public bool CheckConfirmationCode(string confirmation_code)
        {
            return License.Status.CheckConfirmationCode(License.Status.HardwareID,
            confirmation_code);

        }

        /// <summary>
        /// 重新激活许可证
        /// </summary>
        /// <param name="reactivation_code"></param>
        /// <returns></returns>
        public bool ReactivateLicense(string reactivation_code)
        {
            return License.Status.ReactivateLicense(reactivation_code);
        }

        /// <summary>
        /// 创建许可证
        /// </summary>
        /// <param name="project_filename"></param>
        //private void CreateLicenseFile()
        //{
        //    LicenseGenerator licensegen = new LicenseGenerator();
        //    licensegen.AddAdditonalLicenseInformation("Company", "Eye");
        //    licensegen.Hardware_Enabled = true;
        //    licensegen.HardwareID = "1234-1234-1234-1234-1234";
        //    licensegen.CreateLicenseFile(@"C:\newlicense.license");
        //}
    }
}
